package com.lws.basic.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;


import com.lws.basic.R;
import com.lws.basic.utils.activity.AppManager;

import com.lws.basic.utils.common.LoginUtils;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.PhoneStateUtils;
import com.lws.basic.utils.common.SharePreferenceUtils;
import com.lws.basic.utils.common.UIUtils;
import com.lws.basic.view.LoadingViewDialog;

import static com.lws.basic.utils.common.UIUtils.setStatusBarColor;


/**
 * Activity基类,可以在这里注入公共操作器
 */
public abstract class BaseActivity extends AppCompatActivity {
    private LoadingViewDialog mLoadingView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getWindow().requestFeature(Window.FEATURE_NO_TITLE);//隐藏标题
        //界面中如果有EditText，默认隐藏输入法
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);//隐藏输入法
        checkNetworked();//检查网络
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//锁定竖屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);   //应用运行时，保持屏幕高亮，不锁屏
        UIUtils.setTransparentForWindow(getWindow(),false);
        //setStatusBarColor(getResources().getColor(R.color.color_main),this );
        setContentView(setLayoutId());
        initData();
        AppManager.getInstance().addActivity(this);
    }



    /**
     * @return
     * 导入布局
     */
    public abstract int setLayoutId();

    /**
     * 初始化控件,并添加点击滑动等视图事件
     */
    public void initData() {}

    public void signIn() {
       // LoginUtils.clearLogin(this);
        SharePreferenceUtils.putBoolean(this, "is_login", false);
        Intent intent = new Intent();
        intent.setAction("com.lws.login");
        startActivity(intent);
    }

    protected boolean checkNetworked() {
        if (!PhoneStateUtils.checkNetwork(this)) {
            MyToast.show(getApplicationContext(), "手机无可用网络！");
            return false;
        }
        return true;
    }
    public void showLoading(){
        try {
            mLoadingView = new LoadingViewDialog();
            mLoadingView.show(getSupportFragmentManager(), "");
        } catch (Exception e) {
            MyToast.show(e.getMessage());
        }
    }
    public void showLoading(String content){
        try {
            mLoadingView = new LoadingViewDialog(content);
            mLoadingView.show(getSupportFragmentManager(), "");
        }catch (Exception e){
            MyToast.show(e.getMessage());
        }


    }

    public void dismissLoading(){
        try {
            if (mLoadingView != null && mLoadingView.isAdded()) {
                mLoadingView.dismiss();
            }
        }catch (Exception e){
            MyToast.show(e.getMessage());
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getInstance().removeActivity(this);
    }
}
